package com.zllkj.mm.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import androidx.appcompat.widget.*
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.model.entity.OrderListItem
import com.zllkj.mm.module.shopping.vm.ItemShoppingCartIListVM
import com.zllkj.mm.util.JsonUtil
import com.zllkj.mm.util.common.FormatUtil
import java.math.BigDecimal

class OrderConfirmListAdapter(layoutResId: Int, data: List<ItemShoppingCartIListVM?>?) :
        BaseQuickAdapter<ItemShoppingCartIListVM, BaseViewHolder>(layoutResId, data) {


    override fun convert(baseViewHolder: BaseViewHolder, vm: ItemShoppingCartIListVM) {

        Log.d("acthome","-->" + JsonUtil.GsonString(vm.goodsList[0]))


        baseViewHolder.getView<AppCompatTextView>(R.id.tv_shopping_cart_item_shopname).text = vm.shopName
        var result = vm.let {
            var count = 0
            it.goodsList.map { count +=it.goodsNum }
            count
        }

        var price = vm.let {
            var price = 0
            it.goodsList.map { price +=it.unitPrice }
            price
        }



        baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_order_confirm_price_title).text ="共" + result + "件,小计"
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_item_order_confirm_price_content).text = FormatUtil.bigDecimalUtilFormat(price.toString(),"100")

        var goodsArea = baseViewHolder.getView<LinearLayoutCompat>(R.id.ll_each_one_goods_shopping_cart_area)
        goodsArea.removeAllViews()

        vm.goodsList.mapIndexed { index , it ->
            var view = LayoutInflater.from(mContext).inflate(R.layout.item_oach_one_shopping_cart,null)

            Glide.with(mContext).load(ClientConfig.IMG_URL + it.goodsPic).into(view.findViewById<AppCompatImageView>(R.id.iv_each_one_goods_image))
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_name).text = it.goodsName
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_count).text ="x " + it.goodsNum.toString()
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_unitPrice).text = FormatUtil.bigDecimalUtilFormat(it.unitPrice.toString(),"100")
            view.findViewById<AppCompatCheckBox>(R.id.rb_each_one_goods_check).visibility = View.INVISIBLE
            goodsArea.addView(view)
        }

    }


}